﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class WarehouseTransferRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNewSearch;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected HtmlForm F13811;
        protected GridView gvWarehouseTransferRpt;
        protected GridView gvWarehouseTransferRptShowDate;
        protected GridView gvWarehouseTransferRptShowLot;
        protected GridView gvWarehouseTransferRptShowLotShowDate;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblAllocateRule;
        protected Label lblMsg;
        protected Label lblTotalQty;
        protected Label lblTotalQtyText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalQty;
        protected UpdatePanel up1;

        public void BindData2StockList(WarehouseTransferRptQueryEntity warehouseTransferRptQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(warehouseTransferRptQuery.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetWarehouseTransferRptDateSet(warehouseTransferRptQuery);
            if (warehouseTransferRptQuery.ShowLot == "Y")
            {
                if (warehouseTransferRptQuery.ShowDate == "N")
                {
                    base.BindData2GridView(this.gvWarehouseTransferRptShowLot, ds);
                }
                else
                {
                    base.BindData2GridView(this.gvWarehouseTransferRptShowLotShowDate, ds);
                }
            }
            else if (warehouseTransferRptQuery.ShowDate == "N")
            {
                base.BindData2GridView(this.gvWarehouseTransferRpt, ds);
            }
            else
            {
                base.BindData2GridView(this.gvWarehouseTransferRptShowDate, ds);
            }
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            WarehouseTransferRptQueryEntity warehouseTransferRptQuery = this.GetWarehouseTransferRptQueryEntity();
            warehouseTransferRptQuery.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = warehouseTransferRptQuery.CurrentPage;
            warehouseTransferRptQuery.CurrentPage = "0";
            this.BindData2StockList(warehouseTransferRptQuery);
            warehouseTransferRptQuery.IsGetAll = false;
            warehouseTransferRptQuery.CurrentPage = currentPageIndex;
            this.SetWarehouseTransferRptQueryEntity(warehouseTransferRptQuery);
            if (warehouseTransferRptQuery.ShowLot == "Y")
            {
                if (warehouseTransferRptQuery.ShowDate == "N")
                {
                    this.gvWarehouseTransferRptShowLot.Columns[1].Visible = false;
                    this.gvWarehouseTransferRptShowLot.AllowSorting = false;
                    this.gvWarehouseTransferRptShowLot.ShowFooter = false;
                    base.Export2Excel("WarehouseTransferRpt.xls", this.gvWarehouseTransferRptShowLot);
                }
                else
                {
                    this.gvWarehouseTransferRptShowLotShowDate.Columns[1].Visible = false;
                    this.gvWarehouseTransferRptShowLotShowDate.AllowSorting = false;
                    this.gvWarehouseTransferRptShowLotShowDate.ShowFooter = false;
                    base.Export2Excel("WarehouseTransferRpt.xls", this.gvWarehouseTransferRptShowLotShowDate);
                }
            }
            else if (warehouseTransferRptQuery.ShowDate == "N")
            {
                this.gvWarehouseTransferRpt.Columns[1].Visible = false;
                this.gvWarehouseTransferRpt.AllowSorting = false;
                this.gvWarehouseTransferRpt.ShowFooter = false;
                base.Export2Excel("WarehouseTransferRpt.xls", this.gvWarehouseTransferRpt);
            }
            else
            {
                this.gvWarehouseTransferRptShowDate.Columns[1].Visible = false;
                this.gvWarehouseTransferRptShowDate.AllowSorting = false;
                this.gvWarehouseTransferRptShowDate.ShowFooter = false;
                base.Export2Excel("WarehouseTransferRpt.xls", this.gvWarehouseTransferRptShowDate);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            WarehouseTransferRptQueryEntity warehouseTransferRptQuery = this.GetWarehouseTransferRptQueryEntity();
            warehouseTransferRptQuery.CurrentPage = "0";
            this.BindData2StockList(warehouseTransferRptQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            WarehouseTransferRptQueryEntity warehouseTransferRptQuery = this.GetWarehouseTransferRptQueryEntity();
            warehouseTransferRptQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2StockList(warehouseTransferRptQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            WarehouseTransferRptQueryEntity warehouseTransferRptQuery = this.GetWarehouseTransferRptQueryEntity();
            warehouseTransferRptQuery.SrcWhDbID = "";
            warehouseTransferRptQuery.DestWhDbID = "";
            warehouseTransferRptQuery.OwnerID = "";
            warehouseTransferRptQuery.ActualShipDate = "";
            warehouseTransferRptQuery.SkuClassID = "";
            warehouseTransferRptQuery.Descr = "";
            warehouseTransferRptQuery.SkuID = "";
            warehouseTransferRptQuery.IntransitID = "";
            warehouseTransferRptQuery.ReceiptID = "";
            warehouseTransferRptQuery.OrderID = "";
            warehouseTransferRptQuery.ShowDate = "N";
            warehouseTransferRptQuery.ShowLot = "N";
            warehouseTransferRptQuery.LOT_ATTR01 = "";
            warehouseTransferRptQuery.LOT_ATTR02 = "";
            warehouseTransferRptQuery.LOT_ATTR03 = "";
            warehouseTransferRptQuery.LOT_ATTR04 = "";
            warehouseTransferRptQuery.LOT_ATTR05 = "";
            warehouseTransferRptQuery.LOT_ATTR06 = "";
            warehouseTransferRptQuery.LOT_ATTR07 = "";
            warehouseTransferRptQuery.LOT_ATTR08 = "";
            warehouseTransferRptQuery.LOT_ATTR09 = "";
            warehouseTransferRptQuery.LOT_ATTR10 = "";
            warehouseTransferRptQuery.EXPIRY_DATE = "";
            warehouseTransferRptQuery.PRODUCE_DATE = "";
            warehouseTransferRptQuery.EXTERNAL_LOT = "";
            this.BindData2StockList(warehouseTransferRptQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            WarehouseTransferRptQueryEntity warehouseTransferRptQuery = this.GetWarehouseTransferRptQueryEntity();
            if (int.Parse(warehouseTransferRptQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                warehouseTransferRptQuery.CurrentPage = (int.Parse(warehouseTransferRptQuery.CurrentPage) + 1).ToString();
                this.BindData2StockList(warehouseTransferRptQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            WarehouseTransferRptQueryEntity warehouseTransferRptQuery = this.GetWarehouseTransferRptQueryEntity();
            if (int.Parse(warehouseTransferRptQuery.CurrentPage) > 0)
            {
                warehouseTransferRptQuery.CurrentPage = (int.Parse(warehouseTransferRptQuery.CurrentPage) - 1).ToString();
                this.BindData2StockList(warehouseTransferRptQuery);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            WarehouseTransferRptQueryEntity warehouseTransferRptQuery = this.GetWarehouseTransferRptQueryEntity();
            this.BindData2StockList(warehouseTransferRptQuery);
        }

        private WarehouseTransferRptQueryEntity GetWarehouseTransferRptQueryEntity()
        {
            return (base.PageQueryEntitySession as WarehouseTransferRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                WarehouseTransferRptQueryEntity warehouseTransferRptQuery = this.GetWarehouseTransferRptQueryEntity();
                warehouseTransferRptQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2StockList(warehouseTransferRptQuery);
            }
        }

        protected void gvWarehouseTransferRpt_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F13811", this.gvWarehouseTransferRpt);
        }

        protected void gvWarehouseTransferRpt_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr2"));
                }
                if (((Label) e.Row.FindControl("lblQty")) != null)
                {
                    this.TotalQty += decimal.Parse((((Label) e.Row.FindControl("lblQty")).Text != "") ? ((Label) e.Row.FindControl("lblQty")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalQtyText.Text = base.GetPreferenceDigit(this.TotalQty);
            }
        }

        protected void gvWarehouseTransferRpt_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                WarehouseTransferRptQueryEntity warehouseTransferRptQuery = this.GetWarehouseTransferRptQueryEntity();
                warehouseTransferRptQuery.SortField = e.SortExpression;
                if (warehouseTransferRptQuery.SortDirection == "ASC")
                {
                    warehouseTransferRptQuery.SortDirection = "DESC";
                }
                else if (warehouseTransferRptQuery.SortDirection == "DESC")
                {
                    warehouseTransferRptQuery.SortDirection = "ASC";
                }
                this.BindData2StockList(warehouseTransferRptQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void gvWarehouseTransferRptShowDate_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F13811", this.gvWarehouseTransferRptShowDate);
        }

        protected void gvWarehouseTransferRptShowLot_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F13811", this.gvWarehouseTransferRptShowLot);
        }

        protected void gvWarehouseTransferRptShowLotShowDate_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F13811", this.gvWarehouseTransferRptShowLotShowDate);
        }

        private void LoadData()
        {
            WarehouseTransferRptQueryEntity warehouseTransferRptQuery = new WarehouseTransferRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    warehouseTransferRptQuery.SrcWhDbID = this.PreviousPage.SrcWhDBID;
                    warehouseTransferRptQuery.DestWhDbID = this.PreviousPage.DestWhDBID;
                    warehouseTransferRptQuery.OwnerID = this.PreviousPage.OwnerID;
                    warehouseTransferRptQuery.ActualShipDate = this.PreviousPage.ActualShipDate;
                    warehouseTransferRptQuery.SkuClassID = this.PreviousPage.SkuClassID;
                    warehouseTransferRptQuery.Descr = this.PreviousPage.SkuDescr;
                    warehouseTransferRptQuery.SkuID = this.PreviousPage.SkuID;
                    warehouseTransferRptQuery.IntransitID = this.PreviousPage.IntransitID;
                    warehouseTransferRptQuery.ReceiptID = this.PreviousPage.ReceiptID;
                    warehouseTransferRptQuery.OrderID = this.PreviousPage.OrderID;
                    warehouseTransferRptQuery.ShowDate = this.PreviousPage.ShowDate;
                    warehouseTransferRptQuery.ShowLot = this.PreviousPage.ShowLot;
                    warehouseTransferRptQuery.LOT_ATTR01 = this.PreviousPage.LotAttribute01;
                    warehouseTransferRptQuery.LOT_ATTR02 = this.PreviousPage.LotAttribute02;
                    warehouseTransferRptQuery.LOT_ATTR03 = this.PreviousPage.LotAttribute03;
                    warehouseTransferRptQuery.LOT_ATTR04 = this.PreviousPage.LotAttribute04;
                    warehouseTransferRptQuery.LOT_ATTR05 = this.PreviousPage.LotAttribute05;
                    warehouseTransferRptQuery.LOT_ATTR06 = this.PreviousPage.LotAttribute06;
                    warehouseTransferRptQuery.LOT_ATTR07 = this.PreviousPage.LotAttribute07;
                    warehouseTransferRptQuery.LOT_ATTR08 = this.PreviousPage.LotAttribute08;
                    warehouseTransferRptQuery.LOT_ATTR09 = this.PreviousPage.LotAttribute09;
                    warehouseTransferRptQuery.LOT_ATTR10 = this.PreviousPage.LotAttribute10;
                    warehouseTransferRptQuery.EXPIRY_DATE = this.PreviousPage.ExpiryDate;
                    warehouseTransferRptQuery.PRODUCE_DATE = this.PreviousPage.ProduceDate;
                }
                else
                {
                    warehouseTransferRptQuery.SrcWhDbID = "";
                    warehouseTransferRptQuery.DestWhDbID = "";
                    warehouseTransferRptQuery.OwnerID = "";
                    warehouseTransferRptQuery.ActualShipDate = "";
                    warehouseTransferRptQuery.SkuClassID = "";
                    warehouseTransferRptQuery.Descr = "";
                    warehouseTransferRptQuery.SkuID = "";
                    warehouseTransferRptQuery.IntransitID = "";
                    warehouseTransferRptQuery.ReceiptID = "";
                    warehouseTransferRptQuery.OrderID = "";
                    warehouseTransferRptQuery.ShowDate = "N";
                    warehouseTransferRptQuery.ShowLot = "N";
                    warehouseTransferRptQuery.LOT_ATTR01 = "";
                    warehouseTransferRptQuery.LOT_ATTR02 = "";
                    warehouseTransferRptQuery.LOT_ATTR03 = "";
                    warehouseTransferRptQuery.LOT_ATTR04 = "";
                    warehouseTransferRptQuery.LOT_ATTR05 = "";
                    warehouseTransferRptQuery.LOT_ATTR06 = "";
                    warehouseTransferRptQuery.LOT_ATTR07 = "";
                    warehouseTransferRptQuery.LOT_ATTR08 = "";
                    warehouseTransferRptQuery.LOT_ATTR09 = "";
                    warehouseTransferRptQuery.LOT_ATTR10 = "";
                    warehouseTransferRptQuery.EXPIRY_DATE = "";
                    warehouseTransferRptQuery.PRODUCE_DATE = "";
                    warehouseTransferRptQuery.EXTERNAL_LOT = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (warehouseTransferRptQuery.ShowLot == "Y")
            {
                if (warehouseTransferRptQuery.ShowDate == "N")
                {
                    warehouseTransferRptQuery.PageSize = this.gvWarehouseTransferRptShowLot.PageSize.ToString();
                    this.PagerFooter.PageSize = this.gvWarehouseTransferRptShowLot.PageSize;
                }
                else
                {
                    warehouseTransferRptQuery.PageSize = this.gvWarehouseTransferRptShowLotShowDate.PageSize.ToString();
                    this.PagerFooter.PageSize = this.gvWarehouseTransferRptShowLotShowDate.PageSize;
                }
            }
            else if (warehouseTransferRptQuery.ShowDate == "N")
            {
                warehouseTransferRptQuery.PageSize = this.gvWarehouseTransferRpt.PageSize.ToString();
                this.PagerFooter.PageSize = this.gvWarehouseTransferRpt.PageSize;
            }
            else
            {
                warehouseTransferRptQuery.PageSize = this.gvWarehouseTransferRptShowDate.PageSize.ToString();
                this.PagerFooter.PageSize = this.gvWarehouseTransferRptShowDate.PageSize;
            }
            warehouseTransferRptQuery.IsGetAll = false;
            warehouseTransferRptQuery.CurrentPage = "0";
            warehouseTransferRptQuery.SortField = "OWNER_ID,SKU_ID";
            warehouseTransferRptQuery.SortDirection = "ASC";
            this.BindData2StockList(warehouseTransferRptQuery);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvWarehouseTransferRpt);
                base.InitAnalyticsReportGridview(this.gvWarehouseTransferRptShowLot);
                base.InitAnalyticsReportGridview(this.gvWarehouseTransferRptShowDate);
                base.InitAnalyticsReportGridview(this.gvWarehouseTransferRptShowLotShowDate);
                this.LoadData();
            }
        }

        public void SetWarehouseTransferRptQueryEntity(WarehouseTransferRptQueryEntity warehouseTransferRptQuery)
        {
            base.PageQueryEntitySession = warehouseTransferRptQuery;
        }

        public WarehouseTransferRptQuery PreviousPage
        {
            get
            {
                return (WarehouseTransferRptQuery) base.PreviousPage;
            }
        }
    }
}

